import { defineStore } from 'pinia';

/**
 * 路由缓存列表
 */
export const useKeepALiveNames = defineStore('keepALiveNames', {
  state: () => ({
    cachedViews: [],
  }),
  actions: {
    async setCacheKeepAlive(data) {
      this.cachedViews = data;
    },
    async addCachedView(_route) {
      if (_route !== null && _route !== undefined && _route !== '' && _route.meta.keepAlive && this.cachedViews.indexOf(_route.name) === -1) {
        this.cachedViews.push(_route.name);
      };
    },
    async delCachedView(_route) {
      const index = this.cachedViews.indexOf(_route.name);
      index > -1 && this.cachedViews.splice(index, 1);
    },
    async delCachedViews(_routes) {
      if (_routes !== null && _routes !== undefined && _routes !== '') {
        const routeNames = _routes.map((item) => item.name);
        this.cachedViews = this.cachedViews.filter((item) => !routeNames.includes(item));
      }
    },
    async delOthersCachedViews(_route) {
      if (_route.meta.keepAlive) this.cachedViews = [_route.name];
      else this.cachedViews = [];
    },
    async delAllCachedViews() {
      this.cachedViews = [];
    },
  },
});
